//百度坐标转高德（传入经度、纬度）
function bd_decrypt(bd_lng, bd_lat) {
	var X_PI = Math.PI * 3000.0 / 180.0;
	var x = bd_lng - 0.0065;
	var y = bd_lat - 0.006;
	var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
	var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
	var gg_lng = z * Math.cos(theta);
	var gg_lat = z * Math.sin(theta);
	return {
		lng: gg_lng,
		lat: gg_lat
	}
}
//高德坐标转百度（传入经度、纬度）
function bd_encrypt(gg_lng, gg_lat) {
	var X_PI = Math.PI * 3000.0 / 180.0;
	var x = gg_lng,
		y = gg_lat;
	var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
	var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
	var bd_lng = z * Math.cos(theta) + 0.0065;
	var bd_lat = z * Math.sin(theta) + 0.006;
	return {
		bd_lat: bd_lat,
		bd_lng: bd_lng
	};
}

/**
 * 时间戳转时间格式
 * @param {Object} datetime
 * @param {Object} startType
 */
export function getDate(datetime, startType) {
	var date = new Date(datetime); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var year = date.getFullYear(),
		month = ("0" + (date.getMonth() + 1)).slice(-2),
		sdate = ("0" + date.getDate()).slice(-2),
		hour = ("0" + date.getHours()).slice(-2),
		minute = ("0" + date.getMinutes()).slice(-2),
		second = ("0" + date.getSeconds()).slice(-2);
	// 拼接
	// var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
	// 返回
	// return result;
	let resStr = "";
	if (startType === "year")
		resStr =
		year +
		"-" +
		month +
		"-" +
		sdate +
		" " +
		hour +
		":" +
		minute +
		":" +
		second;
	if (startType === "day") resStr = year + "-" + month + "-" + sdate;
	if (startType === "month") resStr = month + "-" + sdate;
	if (startType === "minute") resStr = hour + ":" + minute;
	if (startType === "second") resStr = hour + ":" + minute + ":" + second;
	return resStr;
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


export default {
	bd_decrypt,
	bd_encrypt,
	getDate
};
